﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Framework.Exceptions.RepositoryExceptions;
using HelperTools;
using Interfaces.Criteria;
using Interfaces.Filters;
using Interfaces.Models;
using Interfaces.Providers;
using Interfaces.Repositories;
using Interfaces.RepositoryModels.InternalModels.EconomicInformationModels;

namespace SQLRepositories.EconomicRepositories
{
    public class SectorRepository : EntityBaseRepository<SectorModel>, IEconomicDataProvider<IEnumerable<IIndustrySector>, ISectorsFilter, IBaseCriteria>, ISectorRepository
    {
        public SectorRepository(IDBEconomicInformationServiceContext context) : base(context)
        {}


        public void Create(SectorModel item)
        {
            Context.Sectors.Add(item);
            Context.SaveChanges();
        }

        public SectorModel GetById(Guid id)
        {
            var result = from sector in Context.Sectors
                         where sector.Id.Equals(id) select sector;
            return result.FirstOrDefault();
        }

        public void Delete(Guid id)
        {
            var item = (from sector in Context.Sectors 
                        where sector.Id.Equals(id) select sector).FirstOrDefault();
            if(item == null)
                throw new EntityNotFoundException("Sector");
            Context.Sectors.Remove(item);
            Context.SaveChanges();
        }

        public void Delete(SectorModel item)
        {
            Context.Sectors.Remove(item);
            Context.SaveChanges();
        }

        public IEnumerable<SectorModel> GetAll()
        {
            return Context.Sectors;
        }

        public IEnumerable<IIndustrySector> GetData(IBaseCriteria criteria, ISectorsFilter filter)
        {
            IEnumerable<IIndustrySector> result = null;
            if(filter == null)
                throw new ArgumentNullException("filter");
            if (criteria == null)
                throw new ArgumentNullException("criteria");

            RepositoryMappingHelper.ResetMapping();
            RepositoryMappingHelper.CreateQuoteMapping();
            RepositoryMappingHelper.CreateCompanyMapping(criteria.IsFullLoading, filter
                                    .With(x => x.IndustryFilter)
                                    .With(x => x.CompanyFilter)
                                    .With(x => x.QuoteisFilter));
            RepositoryMappingHelper.CreateIndustryMapping(criteria.IsFullLoading, filter
                                    .With(x => x.IndustryFilter)
                                    .With(x => x.CompanyFilter));
            RepositoryMappingHelper.CreateSectorsMapping(criteria.IsFullLoading, filter
                                    .With(x => x.IndustryFilter));
            
            result = Mapper.Map<IEnumerable<SectorModel>, IEnumerable<IIndustrySector>>(Context.Sectors);
            return filter.Filtrate(result) as IEnumerable<IIndustrySector>;
        }
    }
}
